<?php
/*
$JA#COPYRIGHT$
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

class modJaJobboard_Menu
{
	function getReturnURL($params, $type)
	{
		// Redirect to login
		$uri = JFactory::getURI();
		$url = $uri->toString();
		
		$user = JFactory::getUser();
		$userType = getCurrentUserType($user->id);
		if( $userType == JBJOB_SEEKER_USER ){
			if( $params->get("jseeker_logout", 0) ){
				$url = modJaJobboard_Menu::getRedirectionURL( $params->get("jseeker_logout", 0) );
			}
		}
		if( $userType == JBEMPLOYER_USER ){
			if( $params->get("epl_logout", 0) ){
				$url = modJaJobboard_Menu::getRedirectionURL( $params->get("epl_logout", 0) );
			}
		}
		
		return base64_encode($url);
	}
	
	function redirectToPage($params){
		// Check session
		$user = JFactory::getUser();
		$currentSession = JSession::getInstance('none',array());
        $checked = $currentSession->get("jajobboard_menu_redirect", 0);
        if($checked || $user->id == 0 )
        	return "";
        
        // Set redirect page
		global $mainframe;
		$userType = getCurrentUserType($user->id);
		$page = "";
		
		switch ($userType){
			case JBJOB_SEEKER_USER:
				if( $params->get("jseeker_login", 0) ){
					$page = modJaJobboard_Menu::getRedirectionURL( $params->get("jseeker_login", 0) );
				}
				break;
			case JBEMPLOYER_USER:
				if( $params->get("epl_login", 0) ){
					$page = modJaJobboard_Menu::getRedirectionURL( $params->get("epl_login", 0) );
				}
				break;
			default:
				if( $params->get("guest_login", 0) ){
					$page = modJaJobboard_Menu::getRedirectionURL( $params->get("guest_login", 0) );
				}
		}
		
		if( $page != "" ){
			$currentSession->set("jajobboard_menu_redirect", 1);
			$mainframe->redirect( JRoute::_( $page ) );
		}
	}
	
	function getRedirectionURL($itemID){
		$menu =& JSite::getMenu();
		$item = $menu->getItem($itemID);
		$url = JRoute::_($item->link.'&Itemid='.$itemID, false);
		return $url;
	}
	
	function getType(){
		$user = & JFactory::getUser();
		return (!$user->get('guest')) ? 'logout' : 'login';
	}
	

	function Install_Menu_DB(){		
		if(isset($_SESSION[md5('installed_db_jbmenu')])) return ;
		$_SESSION[md5('installed_db_jbmenu')] = true;
		
		$db = JFactory::getDBO();		
		
		$q = "SELECT data FROM #__ja_config where tab='others'";
		$db->setQuery( $q );
		$data = $db->loadResult();
		$param = new JParameter($data);
		//if($param->get('installed_db_jbmenu', 0)) return;

		$sql = "SELECT count(id) FROM #__menu_types where menutype in ('employer-menus', 'job-seeker-menus', 'default-menus')";
		$db->setQuery( $sql );
		$count = $db->loadResult();
		if($count>0) return;
		
		require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'helper'.DS.'jafunctions.class.php');		
			
		$path = dirname(__FILE__).DS.'installer'.DS.'sql'.DS.'install.mod_jajobboard_menu.sql';
		$error = null;
		if(file_exists($path)){
			populateDB ($path, $db, $error);
			if($error){
				$error = implode("<br/>", $error);
				return JError::raiseError(1, $error);
			}
			else{			
				modJaJobboard_Menu::create_employer_menu();
				$param->set('installed_db_jbmenu', 1);
				$data = $param->toString();
				
				$sql = "Update #__ja_config Set data='$data' Where tab='others'";
				$db->setQuery($sql);
				//$db->query();
			}
		}
		
	} 
	
	function create_employer_menu(){
		$db = JFactory::getDBO();	
		// Insert employer menu
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'View Resumes', 'employer-resumes', 'index.php?option=com_jajobboard&view=jaresumes&layout=jalist', 'component', 1, 0, 36, 0, 3, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'Favourite resumes', 'favourite-resumes', 'index.php?option=com_jajobboard&view=jaresumes&layout=jaemployerlist', 'component', 1, ".$db->insertid().", 36, 1, 1, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query() or die('phuong dien');
		
		
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'View Applications', 'application', 'index.php?option=com_jajobboard&view=jaapplications&layout=jaempllist', 'component', 1, 0, 36, 0, 4, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		
		
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'View Job Posts', 'posts', 'index.php?option=com_jajobboard&view=jajobs&layout=myjobs', 'component', 1, 0, 36, 0, 5, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		$parent_id = $db->insertid();
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'My posted jobs', 'my-posted-jobs', 'index.php?option=com_jajobboard&view=jajobs&layout=myjobs', 'component', 1, ".$parent_id.", 36, 1, 1, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'Post a New Job', 'post-a-new-job', 'index.php?option=com_jajobboard&view=jajobs&layout=jaform', 'component', 1, ".$parent_id.", 36, 1, 2, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'Post a Premium Job', 'post-a-premium-job', 'index.php?option=com_jajobboard&view=jajobs&layout=japremiumform', 'component', 1, ".$parent_id.", 36, 1, 3, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'Buy Credit', 'buy-credit', 'index.php?option=com_jajobboard&view=japosts&layout=buycredit', 'component', 1, ".$parent_id.", 36, 1, 4, 0, '0000-00-00 00:00:00', 0, 0, 0, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'Buy posting plan', 'buy-posting-plan', 'index.php?option=com_jajobboard&view=japosts&layout=selectplan', 'component', 1, ".$parent_id.", 36, 1, 5, 0, '0000-00-00 00:00:00', 0, 0, 0, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		
		
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'My Cpanel', 'my-ecpanel', 'index.php?option=com_jajobboard&view=jaemployers&layout=account', 'component', 1, 0, 36, 0, 6, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		$parent_id = $db->insertid();
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'Account Information', 'my-account', 'index.php?option=com_jajobboard&view=jaemployers&layout=account', 'component', 1, ".$parent_id.", 36, 1, 1, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'My Profile', 'my-profile', 'index.php?option=com_jajobboard&view=japrofiles&layout=jaview', 'component', 1, ".$parent_id.", 36, 1, 3, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
		$sql = "INSERT INTO #__menu (`id`, `menutype`, `name`, `alias`, `link`, `type`, `published`, `parent`, `componentid`, `sublevel`, `ordering`, `checked_out`, `checked_out_time`, `pollid`, `browserNav`, `access`, `utaccess`, `params`, `lft`, `rgt`, `home`) VALUES ";
		$sql .= "(NULL, 'employer-menus', 'Transactions History', 'my-transactions', 'index.php?option=com_jajobboard&view=jaemployers&layout=transactions', 'component', 1, ".$parent_id.", 36, 1, 4, 0, '0000-00-00 00:00:00', 0, 0, 1, 0, 'page_title=\nshow_page_title=1\npageclass_sfx=\nmenu_image=-1\nsecure=0\n\n', 0, 0, 0)";
		$db->setQuery($sql);
		$db->query();
	}
}
